Programming in C by Stephen G. Kochan

Programming in C by Stephen G. Kochan

Author:Stephen G. Kochan
Language: eng
Format: epub, pdf
ISBN: 9780672331411
Publisher: Sams Publishing


The array days is defined to contain seven entries, each a pointer to a character string. So days[0] contains a pointer to the character string "Sunday", days[1] contains a pointer to the string "Monday", and so on (see Figure 11.10). You could display the name of the third weekday, for example, with the following statement:

printf ("%s\n", days[3]);

Figure 11.10 Array of pointers.

The Increment and Decrement Operators Revisited

Up to this point, whenever you used the increment or decrement operator, it was the only operator that appeared in the expression. When you write the expression ++x, you know that this has the effect of adding 1 to the value of the variable x. And as you have just seen, if x is a pointer to an array, this has the effect of setting x to point to the next element of the array.

The increment and decrement operators can be used in expressions in which other operators also appear. In such cases, it becomes important to know more precisely how these operators work.

So far, when you used the increment and decrement operators, you always placed them before the variables that were being incremented or decremented. So, to increment a variable i, you simply wrote

++i;

Actually, it also is perfectly valid to place the increment operator after the variable, as follows:

i++;

Both expressions are perfectly valid and both achieve the same result—namely, of incrementing the value of i. In the first case, where the ++ is placed before its operand, the increment operation is more precisely identified as a preincrement. In the second case, where the ++ is placed after its operand, the operation is identified as a postincrement.

The same discussion applies to the decrement operator. So the statement

--i;

technically performs a predecrement of i, whereas the statement

i--;

performs a postdecrement of i. Both have the same net result of subtracting 1 from the value of i.

It is when the increment and decrement operators are used in more complex expressions that the distinction between the pre- and post- nature of these operators is realized.

Suppose you have two integers called i and j. If you set the value of i to 0 and then write the statement

j = ++i;

the value that gets assigned to j is 1, and not 0 as you might expect. In the case of the preincrement operator, the variable is incremented before its value is used in the expression. So, in the preceding expression, the value of i is first incremented from 0 to 1 and then its value is assigned to j, as if the following two statements had been written instead:

++i;

j = i;

If you instead use the postincrement operator in the statement

j = i++;

then i is incremented after its value has been assigned to j. So, if i is 0 before the preceding statement is executed, 0 is assigned to j and then i is incremented by 1, as if the statements

j = i;

++i;

were used instead. As another example, if i is equal to 1, then the statement

x = a[--i];

has the effect of assigning



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.